package ar.Location.Api.Flickr;

import java.io.IOException;

import org.json.JSONException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.Html;
import android.util.Log;
import android.view.View;
import ar.Location.Helper.BitmapProvider;
import ar.Location.Helper.CalculationProvider;
import ar.Location.Helper.StringHelper;
import ar.Location.PlaceObject.Place;
import ar.Location.UI.PlaceResultItemView;
import ar.activity.R.drawable;
import ar.activity.PlaceDetailActivity;
import ar.globals.Globals;

import com.gmail.yuyang226.flickr.FlickrException;
import com.gmail.yuyang226.flickr.people.PeopleInterface;
import com.gmail.yuyang226.flickr.people.User;
import com.gmail.yuyang226.flickr.photos.GeoData;
import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.PhotosInterface;
import com.gmail.yuyang226.flickr.photos.geo.GeoInterface;

public class FlickrPlace extends Place {

	protected static final String TAG = "FlickrPlace";

	public String views = "";
	public String photoId = "";
	
	public FlickrPlace(String dfName) {
		categoryName = dfName;
		websiteUrl = "http://www.flickr.com/";
	}

	public void updateGeoData(String photoId, GeoInterface geoInterface) {
		// TODO Auto-generated method stub
		try {
			Log.d(TAG, "Start load geodata");
			GeoData geoData = geoInterface.getLocation(photoId);
			if (geoData != null) {
				float lat = geoData.getLatitude();
				float lng = geoData.getLongitude();
				latitude = String.valueOf(lat);
				longitude = String.valueOf(lng);
			}
			Log.d(TAG, "Finish load geodata");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			Log.d(TAG, e.getMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		}
	}
	
	public void updateDescription(String photoId, String secret, PhotosInterface photosInterface) {
		// TODO Auto-generated method stub
		try {
			Log.d(TAG, "Start load description");
			if(photoId != null && secret != null) {
				if(photoId.length() > 0 && secret.length() > 0) {
					Photo photo = photosInterface.getInfo(photoId, secret);
					if(photo != null) {
						if(photo.getDescription() != null && photo.getDescription().length() > 0) {
							description = photo.getDescription();
						}
					}
				}
			}
			Log.d(TAG, "Finish load description");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			Log.d(TAG, e.getMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		}
	}

	public void updateUserInfo(String userId, PeopleInterface peopleInterface) {
		// TODO Auto-generated method stub
		try {
			Log.d(TAG, "Start load user info");
			if(userId != null && userId.length() > 0) {
				User u = peopleInterface.getInfo(userId);
				if(u != null) {
					if(u.getRealName() != null && u.getRealName().length() > 0) {
						user = u.getRealName();
					} else {
						if(u.getUsername() != null && u.getUsername().length() > 0) {
							user = u.getUsername();
						}
					}
				}
			}
			Log.d(TAG, "Finish load user info");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			Log.d(TAG, e.getMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
		}
	}

	@Override
	public void bindDataToViewDetail(final PlaceDetailActivity detailView) {
		// TODO Auto-generated method stub
		if(detailView != null) {
			detailView.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					detailView.imgViewIcon.setImageBitmap(BitmapProvider.bmpIconPhoto);
					
					detailView.txtViewName.setText(name);
					
					if(description != null && description.length() > 0) {
						detailView.txtViewDescription.setText(Html.fromHtml(description));
						if(user != null && user.length() > 0) {
							detailView.txtViewDescription.append("\n\n" + "From: " + user);
						}
					} else {
						if(user != null && user.length() > 0) {
							detailView.txtViewDescription.setText("From: " + user);
						} else {
							detailView.txtViewDescription.setVisibility(View.GONE);
						}
					}
					
					
					if(imageUrl == null || imageUrl.length() == 0) {
						detailView.imgViewThumb.setVisibility(View.GONE);
						detailView.progressBarUpdateThumbImage.setVisibility(View.GONE);
					}
					
					if(latitude.length() > 0 && longitude.length() > 0) {
						float myLat = Float.parseFloat(Globals.LAT);
						float myLng = Float.parseFloat(Globals.LNG);
						float lat = Float.parseFloat(latitude);
						float lng = Float.parseFloat(longitude);
						float dist = CalculationProvider.distFrom(myLat, myLng, lat, lng);
						String strDistance = StringHelper.getStringDistance(Math.round(dist));
						detailView.txtViewDistance.setText(String.valueOf(strDistance));
					}
					else {
						detailView.txtViewDistance.setText("");
					}
					
					if(address != null && address.length() > 0) {
						detailView.txtViewAddress.setText(address);
					} else {
						detailView.txtViewAddress.setVisibility(View.GONE);
						detailView.imgViewBulletAddress.setVisibility(View.GONE);
					}
					
					if(websiteUrl != null && dateTime.length() > 0) {
						detailView.txtViewWebsiteUrl.setText("Website: " + websiteUrl);
					} else {
						detailView.txtViewWebsiteUrl.setVisibility(View.GONE);
						detailView.imgViewBulletWebsite.setVisibility(View.GONE);
					}
					
					if(dateTime != null && dateTime.length() > 0) {
						detailView.txtViewDateTime.setText("From: " + dateTime);
					} else {
						detailView.txtViewDateTime.setVisibility(View.GONE);
						detailView.imgViewBulletDateTime.setVisibility(View.GONE);
					}
					
					detailView.txtViewPhoneNumber.setVisibility(View.GONE);
					detailView.imgViewBulletPhoneNumber.setVisibility(View.GONE);
					detailView.actionCall.setVisibility(View.GONE);
				}
			});
		}
	}

	@Override
	public void bindDataToListViewItem(final PlaceResultItemView listViewItem) {
		// TODO Auto-generated method stub
		if(listViewItem != null) {
			((Activity)listViewItem.listViewItemContext).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					listViewItem.imgIconName.setImageBitmap(BitmapProvider.bmpIconPhoto);
					listViewItem.imgIconDescription.setImageResource(drawable.icon_bullet);
					listViewItem.txtViewName.setText(name);
					if(description != null && description.length() > 0) {
						listViewItem.txtViewDescription.setText(Html.fromHtml(StringHelper.getTruncateString(description)));
					} else {
						listViewItem.txtViewDescription.setText("User: " + user);
					}
					
					if(latitude.length() > 0 && longitude.length() > 0) {
						float myLat = Float.parseFloat(Globals.LAT);
						float myLng = Float.parseFloat(Globals.LNG);
						float lat = Float.parseFloat(latitude);
						float lng = Float.parseFloat(longitude);
						float dist = CalculationProvider.distFrom(myLat, myLng, lat, lng);
						String strDistance = StringHelper.getStringDistance(Math.round(dist));
						listViewItem.txtViewDistance.setText(String.valueOf(strDistance));
					}
					else {
						listViewItem.txtViewDistance.setText("");
					}
				}
			});
		}
	}
	
	public void updateImage(final PlaceResultItemView listViewItem, final Bitmap bmp) {
		// TODO Auto-generated method stub
		if(listViewItem != null) {
			((Activity)listViewItem.listViewItemContext).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					listViewItem.imgView.setVisibility(View.VISIBLE);
					listViewItem.progressBar.setVisibility(View.GONE);
					if(bmp != null) {
						listViewItem.imgView.setImageBitmap(bmp);
					} else {
						listViewItem.imgView.setImageBitmap(null);
					}
				}
			});
		}
		
	}
}
